package com.chinanet.config;

import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 在异步方法中如何获取用户信息
 * SecurityContextHolder的主要功能是将当前执行的进程和SecurityContext关联起来。
 * <p>
 * SecurityContextHolder.MODE_INHERITABLETHREADLOCAL ：用于线程有父子关系的情景中，子线程集成父线程的SecurityContextHolder；
 * <p>
 * SecurityContextHolder.MODE_INHERITABLETHREADLOCAL ：全局共用SecurityContextHolder
 */
@Configuration
public class AuditorAwareConfig {
    @Bean
    public MethodInvokingFactoryBean methodInvokingFactoryBean() {
        MethodInvokingFactoryBean methodInvokingFactoryBean = new MethodInvokingFactoryBean();
        methodInvokingFactoryBean.setTargetClass(SecurityContextHolder.class);
        methodInvokingFactoryBean.setTargetMethod("setStrategyName");
        methodInvokingFactoryBean.setArguments(new String[]{SecurityContextHolder.MODE_INHERITABLETHREADLOCAL});
        return methodInvokingFactoryBean;
    }
}